<?php

if_get('/event_instances', function ()
{/*{{{*/
    list(
        $inputs['event_id']
    ) = input_list(
        'event_id'
    );

    $inputs = array_filter($inputs, 'not_null');

    $event_instances = dao('event_instance')->find_all_by_column($inputs);

    return [
        'count' => count($event_instances),
        'event_instances' => array_build($event_instances, function ($id, $event_instance) {
            return [
                null,
                [
                    'id' => $event_instance->id,
                    'event_display' => $event_instance->event->display_for_event_instances_event(),
                    'create_time' => $event_instance->create_time,
                    'update_time' => $event_instance->update_time,
                ]
            ];
        }),
    ];
});/*}}}*/

if_post('/event_instances/add', function ()
{/*{{{*/

    $event_instance = event_instance::create(
        input_entity('event', null, 'event_id')
    );

    return [
        'id' => $event_instance->id,
    ];
});/*}}}*/

//todo::detail

if_post('/event_instances/update/*', function ($event_instance_id)
{/*{{{*/

    $event_instance = dao('event_instance')->find($event_instance_id);
    otherwise_error_code('EVENT_INSTANCE_NOT_FOUND', $event_instance->is_not_null());

    $event_instance->event = input_entity('event', null, 'event_id');

    return true;
});/*}}}*/

if_post('/event_instances/delete/*', function ($event_instance_id)
{/*{{{*/
    $event_instance = dao('event_instance')->find($event_instance_id);
    otherwise_error_code('EVENT_INSTANCE_NOT_FOUND', $event_instance->is_not_null());

    $event_instance->delete();

    return true;
});/*}}}*/
